home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8627 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  63 lines

  1. Path: isonews.bbn.hp.com!hpbblb!news
  2. From: Matthias Dittrich <matti>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Why doesn't this work?
  5. Date: 5 Mar 1996 14:41:17 GMT
  6. Organization: Hewlett-Packard Co.
  7. Message-ID: <4hhjqd$ki1@hpbblb.bbn.hp.com>
  8. References: <1996Mar4.161412.137442@forest>
  9. NNTP-Posting-Host: trabant.bbn.hp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
  14. X-URL: news:1996Mar4.161412.137442@forest
  15.  
  16. ebromber@forest.drew.edu wrote:
  17. >Does anyone know why this password program doesn't work properly? And if 
  18. >you do know the problem how can I fix it? It rejects every password including 
  19. >the real password. The program was compiled using a MS-DOS compiler.
  20. >Thanks in advance.
  21. >ebromber@drew.edu
  22. >
  23. >main();
  24. >{    char real[4];
  25. >    char pass[100];
  26. >    int count=0;
  27. >    int i, error;
  28. >    char c;
  29. >    real[0]='j';real[1]='e';real[2]='r';real[3]='k';
  30. >    printf("PASSWORD: ");
  31. >    fflush(stdout);
  32. >    while (c=getch() !='\n')
  33. Cosider the precedence order of operators. Here first getch() !='\n' will be
  34. evaluated, then assigned to c. So c contains 0 or 1.
  35. What you want is:
  36. while ((c=getch()) !='\n')
  37.  
  38. >    {     count++;
  39. >        pass[count]=c;
  40. The first value of c will be assigned to pass[1] if you are incrementing
  41. count before this statement.
  42.  
  43. >        putch('*');
  44. >    }
  45. >    if (count!=4) { printf("\nWRONG PASSWORD\n"); main();}
  46. >    error=0;
  47. >    for (i=0; i<4; i++)
  48. >        if (real[i]=pass[i]) error++;
  49. You should increase the error counter if the characters are not equal:
  50.         if (real[i] != pass[i]) error++;
  51.                             ^^
  52. Otherwise it will be incremented if the value of pass[i] is not zero.
  53.  
  54. >    if (error>0) {printf("\nWRONG PASSWORD\n"); main();}
  55. >}
  56. >
  57. At all recursion seems not a good solution in this case. I would prefer
  58. a loop construct using while.
  59.  
  60. Good luck,
  61. Matthias
  62.  
  63.